package com.lynn.common.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * 软键盘工具类,需要事件触发
 */
public class KeyboardUtil {

    private KeyboardUtil() {
    }


    /**
     * 软键盘是否显示
     *
     * @param rootView
     * @return
     */
    public static boolean isKeyboardShown(View rootView) {
        final int softKeyboardHeight = 100;
        Rect r = new Rect();
        rootView.getWindowVisibleDisplayFrame(r);
        DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
        int heightDiff = rootView.getBottom() - r.bottom;
        return heightDiff > softKeyboardHeight * dm.density;
    }

    /**
     * 测量软键盘的底部距离屏幕底部大小
     *
     * @param rootView
     * @return
     */
    public static int getKeyboardToBottomhighet(View rootView) {
        final int softKeyboardHeight = 100;
        Rect r = new Rect();
        rootView.getWindowVisibleDisplayFrame(r);
        DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
        return rootView.getBottom() - r.bottom;
    }


    /**
     * 显示软键盘
     */
    public static void showSoftKeyboard(View view) {
        Context context = view.getContext();
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        toggle(context, view, true);
    }


    /**
     * 隐藏软键盘
     *
     * @param view
     */
    public static void hideSoftKeyboard(View view) {
        Context context = view.getContext();
        toggle(context, view, false);
    }


    /**
     * 显示和隐藏软键盘 View ：
     * EditText、TextView isShow : true = show , false = hide
     *
     * @param view
     * @param isShow
     */
    private static void toggle(Context context, View view, boolean isShow) {
        if (isShow) {
            view.requestFocus();
            getInputMethodManager(context).showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
        } else {
            getInputMethodManager(context).hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }


    public static void show(Activity activity) {
        if (activity != null) {
            toggle(activity, true);
        }
    }

    public static void hide(Activity activity) {
        if (activity != null) {
            toggle(activity, false);
        }
    }

    private static void toggle(Activity context, boolean isShow) {
        if (isShow) {
            getInputMethodManager(context)
                    .showSoftInput(context.getWindow().getDecorView(), InputMethodManager.SHOW_FORCED);
        } else {
            getInputMethodManager(context)
                    .hideSoftInputFromWindow(context.getWindow().getDecorView().getWindowToken(), 0);
        }
    }


    private static InputMethodManager getInputMethodManager(Context context) {
        return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    }


}
